import java.util.Arrays;

public class MyStack {
    public int[] elem;//存放栈的数据
    public int usedSize;//我们数组的有效长度

    public MyStack() {
        this.elem = new int[10];
    }

    private boolean isFull() {
        return usedSize == elem.length;
    }
    public void push(int e) {
        if (isFull()) {
            //二倍扩容
            this.elem = Arrays.copyOf(elem,
                    2*elem.length);
        }
        //这是在放完元素后再自加
        elem[usedSize++] = e;
    }

    public boolean empty() {
        return usedSize == 0;
    }

    public int peek() {
        if (empty()) {
            System.out.println("栈为空没有元素");
            return -1;
        }

        return elem[usedSize - 1];
    }

    public int pop() {
        int e = peek();
        usedSize--;
        return e;
    }

    public int size() {
        return usedSize;
    }

    public void display() {
        System.out.print("[");
        for (int i = 0; i < usedSize; i++) {
            System.out.print(elem[i] + " ");
        }
        System.out.print("]");
    }
}
